﻿using LSQL;
using Shopfloormanagement.CustomControls;
using Sunny.UI;
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using DevExpress.XtraEditors;
using Shopfloormanagement.PDCA;

namespace Shopfloormanagement
{
    public partial class Form_Department : Form
    {
        public static Form_Department department = new Form_Department();
        public int FactoryID;
        public static int DepartmentID;
        private int id_department;
        public int Level;
        int Year = DateTime.Now.Year;
        int Month = DateTime.Now.Month;
        int a = 0;
        public string FrameType = "";
        int LineChartType = 1;
        private List<UILabel> labels;
        private string lbName = string.Empty;
        private string lbText = string.Empty;
        public static string TodoID = string.Empty;
        private string TopicID = string.Empty;
        public static string topicStr = string.Empty;
        public Form_Department()
        {
            InitializeComponent();
            // 初始化 UILabel 列表，手动将设计器中的所有 UILabel 添加进来
            labels = new List<UILabel> { lab_act11, lab_act12, lab_act13, lab_act21, lab_act22, lab_act23, lab_act31, lab_act32, lab_act33 };
            // 为每个 UILabel 添加 Click 事件处理程序
            foreach (var label in labels)
            {
                label.Click += Label_Click;
            }
        }
        private void Form_Department_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            department = this;

            if (DateTime.Now.Day == 1)
            {
                Month = Month - 1;
                dtp_Date.Value = DateTime.Now.AddMonths(-1);
            }
            else
            {
                dtp_Date.Value = DateTime.Now;
            }
            //LoadFrame();
            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "Departments", $"ID = {DepartmentID}");
            if (dt1.Rows.Count > 0)
            {
                lab_Department.Text = dt1.Rows[0][1].ToString();
            }
            else
            {
                lab_Department.Text = "Unknow";
            }
            //LoadTopIssues();
            //LoadOpenItem();
        }
        private void Form_Department_SizeChanged(object sender, EventArgs e)
        {
            //panel1.Height = this.Height - 16;
        }
        private void btn_Back_Click(object sender, EventArgs e)
        {
            this.Close();
            DevMainForm.instance.LoadDepartment(SystemInfo.readLevel, SystemInfo.readFactory, SystemInfo.readparentDep);
        }
        private void LoadFrame()
        {
            //等待测试Layout无错误将下方Interface更改为Layout 2024/12/20
            //DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "Interface", "FactoryID=" + FactoryID + " and DepartmentID=" + DepartmentID + " and Level=" + Level + " and Del_tag=0 order by ControlOrder asc");
            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "Interface", "FactoryID=" + FactoryID + " and DepartmentID=" + DepartmentID + " and Del_tag=0 order by ControlOrder asc");
            DataRow[] rowS = dt1.Select("KPItype='S'");
            DataRow[] rowQ = dt1.Select("KPItype='Q'");
            DataRow[] rowD = dt1.Select("KPItype='D'");
            DataRow[] rowC = dt1.Select("KPItype='C'");
            if (rowS.Length > 0)
            {
                string Title = "S";
                Label myLabel = new Label();
                myLabel.Parent = panel1;
                myLabel.Name = Title;
                myLabel.Text = Title;
                myLabel.AutoSize = true;
                myLabel.ForeColor = Color.Black;
                myLabel.Font = new Font("Arial", 42);
                myLabel.Location = new Point(17, 80 + a);
                panel1.Controls.Add(myLabel);
                a += 10;
                for (int i = 0; i < rowS.Length; i++)
                {
                    if (rowS[i]["ControlType"].ToString() == "红绿表格")
                    {
                        if (FrameType == "折线图")
                        {
                            double.TryParse(rowS[i]["MaxTarget"].ToString(), out double Max);
                            double.TryParse(rowS[i]["MinTarget"].ToString(), out double Min);
                            CreateDGV1(Title + "_红绿表格_" + rowS[i]["ControlOrder"].ToString() + "_折线图", 95 + a, 1, rowS[i]["RowTitle1"].ToString(), rowS[i]["RowTitle2"].ToString(), rowS[i]["ControlType"].ToString(), Convert.ToInt32(rowS[i]["ControlOrder"].ToString()), Title, Max, Min, rowS[i]["Indicator"].ToString(), rowS[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        else
                        {
                            CreateDGV1(Title + "_红绿表格_" + rowS[i]["ControlOrder"].ToString(), 95 + a, 1, rowS[i]["RowTitle1"].ToString(), rowS[i]["RowTitle2"].ToString(), rowS[i]["ControlType"].ToString(), Convert.ToInt32(rowS[i]["ControlOrder"].ToString()), Title, 0, 0, rowS[i]["Indicator"].ToString(), rowS[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        FrameType = "红绿表格";
                    }
                    else if (rowS[i]["ControlType"].ToString() == "文字表格")
                    {
                        if (FrameType == "折线图")
                        {
                            CreateDGV2(Title + "_文字表格_" + rowS[i]["ControlOrder"].ToString() + "_折线图", 95 + a, Convert.ToInt32(rowS[i]["RowType"].ToString()), rowS[i]["RowTitle1"].ToString(), rowS[i]["RowTitle2"].ToString(), rowS[i]["ControlType"].ToString(), Convert.ToInt32(rowS[i]["ControlOrder"].ToString()), Title, rowS[i]["Indicator"].ToString(), rowS[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        else
                        {
                            CreateDGV2(Title + "_文字表格_" + rowS[i]["ControlOrder"].ToString(), 95 + a, Convert.ToInt32(rowS[i]["RowType"].ToString()), rowS[i]["RowTitle1"].ToString(), rowS[i]["RowTitle2"].ToString(), rowS[i]["ControlType"].ToString(), Convert.ToInt32(rowS[i]["ControlOrder"].ToString()), Title, rowS[i]["Indicator"].ToString(), rowS[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        FrameType = "文字表格";
                    }
                    else if (rowS[i]["ControlType"].ToString() == "折线图")
                    {
                        if (LineChartType == 0)
                        {
                            CreateLineChart1(Title + "_折线图_" + rowS[i]["ControlOrder"].ToString(), 95 + a, rowS[i]["ControlType"].ToString(), Convert.ToInt32(rowS[i]["ControlOrder"].ToString()), Title, Convert.ToDouble(rowS[i]["MaxTarget"].ToString()), rowS[i]["Indicator"].ToString(), rowS[i]["Target/Limit"].ToString(), rowS[i]["ControlName"].ToString());
                            a += 170;
                            FrameType = "折线图";
                        }
                        else
                        {
                            CreateLineChart3(Title + "_折线图_" + rowS[i]["ControlOrder"].ToString(), 95 + a, rowS[i]["ControlType"].ToString(), Convert.ToInt32(rowS[i]["ControlOrder"].ToString()), Title, Convert.ToDouble(rowS[i]["MaxTarget"].ToString()), Convert.ToDouble(rowS[i]["MinTarget"].ToString()), rowS[i]["Indicator"].ToString(), rowS[i]["Target/Limit"].ToString(), rowS[i]["ControlName"].ToString());
                            a += 250;
                            FrameType = "折线图";
                        }
                    }
                    else if (rowS[i]["ControlType"].ToString() == "文本表格")
                    {
                        if (FrameType == "红绿表格")
                        {
                            CreateDGV4(Title + "_文本表格_" + rowS[i]["ControlOrder"].ToString(), 95 + a, rowS[i]["ControlType"].ToString(), Convert.ToInt32(rowS[i]["ControlOrder"].ToString()), Title, Convert.ToInt32(rowS[i]["RowType"].ToString()));
                            a += 120;
                        }
                        else
                        {
                            CreateDGV4(Title + "_文本表格_" + rowS[i]["ControlOrder"].ToString(), 95 + a, rowS[i]["ControlType"].ToString(), Convert.ToInt32(rowS[i]["ControlOrder"].ToString()), Title, Convert.ToInt32(rowS[i]["RowType"].ToString()));
                            a += 120;

                        }
                        FrameType = "文本表格";
                    }
                }
            }
            if (rowQ.Length > 0)
            {
                string Title = "Q";
                Label myLabel = new Label();
                myLabel.Parent = panel1;
                myLabel.Name = Title;
                myLabel.Text = Title;
                myLabel.AutoSize = true;
                myLabel.ForeColor = Color.Black;
                myLabel.Font = new Font("Arial", 42);
                myLabel.Location = new Point(17, 80 + a);
                panel1.Controls.Add(myLabel);
                a += 10;
                for (int i = 0; i < rowQ.Length; i++)
                {
                    if (rowQ[i]["ControlType"].ToString() == "红绿表格")
                    {
                        if (FrameType == "折线图")
                        {
                            double.TryParse(rowQ[i]["MaxTarget"].ToString(), out double Max);
                            double.TryParse(rowQ[i]["MinTarget"].ToString(), out double Min);
                            CreateDGV1(Title + "_红绿表格_" + rowQ[i]["ControlOrder"].ToString() + "_折线图", 95 + a, 1, rowQ[i]["RowTitle1"].ToString(), rowQ[i]["RowTitle2"].ToString(), rowQ[i]["ControlType"].ToString(), Convert.ToInt32(rowQ[i]["ControlOrder"].ToString()), Title, Max, Min, rowQ[i]["Indicator"].ToString(), rowQ[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        else
                        {
                            CreateDGV1(Title + "_红绿表格_" + rowQ[i]["ControlOrder"].ToString(), 95 + a, 1, rowQ[i]["RowTitle1"].ToString(), rowQ[i]["RowTitle2"].ToString(), rowQ[i]["ControlType"].ToString(), Convert.ToInt32(rowQ[i]["ControlOrder"].ToString()), Title, 0, 0, rowQ[i]["Indicator"].ToString(), rowQ[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        FrameType = "红绿表格";
                    }
                    else if (rowQ[i]["ControlType"].ToString() == "文字表格")
                    {
                        if (FrameType == "折线图")
                        {
                            CreateDGV2(Title + "_文字表格_" + rowQ[i]["ControlOrder"].ToString() + "_折线图", 95 + a, Convert.ToInt32(rowQ[i]["RowType"].ToString()), rowQ[i]["RowTitle1"].ToString(), rowQ[i]["RowTitle2"].ToString(), rowQ[i]["ControlType"].ToString(), Convert.ToInt32(rowQ[i]["ControlOrder"].ToString()), Title, rowQ[i]["Indicator"].ToString(), rowQ[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        else
                        {
                            CreateDGV2(Title + "_文字表格_" + rowQ[i]["ControlOrder"].ToString(), 95 + a, Convert.ToInt32(rowQ[i]["RowType"].ToString()), rowQ[i]["RowTitle1"].ToString(), rowQ[i]["RowTitle2"].ToString(), rowQ[i]["ControlType"].ToString(), Convert.ToInt32(rowQ[i]["ControlOrder"].ToString()), Title, rowQ[i]["Indicator"].ToString(), rowQ[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        FrameType = "文字表格";
                    }
                    else if (rowQ[i]["ControlType"].ToString() == "折线图")
                    {
                        if (LineChartType == 0)
                        {
                            CreateLineChart1(Title + "_折线图_" + rowQ[i]["ControlOrder"].ToString(), 95 + a, rowQ[i]["ControlType"].ToString(), Convert.ToInt32(rowQ[i]["ControlOrder"].ToString()), Title, Convert.ToDouble(rowQ[i]["MaxTarget"].ToString()), rowQ[i]["Indicator"].ToString(), rowQ[i]["Target/Limit"].ToString(), rowQ[i]["ControlName"].ToString());
                            a += 170;
                            FrameType = "折线图";
                        }
                        else
                        {
                            CreateLineChart3(Title + "_折线图_" + rowQ[i]["ControlOrder"].ToString(), 95 + a, rowQ[i]["ControlType"].ToString(), Convert.ToInt32(rowQ[i]["ControlOrder"].ToString()), Title, Convert.ToDouble(rowQ[i]["MaxTarget"].ToString()), Convert.ToDouble(rowQ[i]["MinTarget"].ToString()), rowQ[i]["Indicator"].ToString(), rowQ[i]["Target/Limit"].ToString(), rowQ[i]["ControlName"].ToString());
                            a += 250;
                            FrameType = "折线图";
                        }
                    }
                    else if (rowQ[i]["ControlType"].ToString() == "文本表格")
                    {
                        if (FrameType == "红绿表格")
                        {
                            CreateDGV4(Title + "_文本表格_" + rowQ[i]["ControlOrder"].ToString(), 95 + a, rowQ[i]["ControlType"].ToString(), Convert.ToInt32(rowQ[i]["ControlOrder"].ToString()), Title, Convert.ToInt32(rowQ[i]["RowType"].ToString()));
                            a += 120;
                        }
                        else
                        {
                            CreateDGV4(Title + "_文本表格_" + rowQ[i]["ControlOrder"].ToString(), 95 + a, rowQ[i]["ControlType"].ToString(), Convert.ToInt32(rowQ[i]["ControlOrder"].ToString()), Title, Convert.ToInt32(rowQ[i]["RowType"].ToString()));
                            a += 120;

                        }
                        FrameType = "文本表格";
                    }
                }
            }
            if (rowD.Length > 0)
            {
                string Title = "D";
                Label myLabel = new Label();
                myLabel.Parent = panel1;
                myLabel.Name = Title;
                myLabel.Text = Title;
                myLabel.AutoSize = true;
                myLabel.ForeColor = Color.Black;
                myLabel.Font = new Font("Arial", 42);
                myLabel.Location = new Point(17, 80 + a);
                panel1.Controls.Add(myLabel);
                a += 10;
                for (int i = 0; i < rowD.Length; i++)
                {
                    if (rowD[i]["ControlType"].ToString() == "红绿表格")
                    {
                        if (FrameType == "折线图")
                        {
                            double.TryParse(rowD[i]["MaxTarget"].ToString(), out double Max);
                            double.TryParse(rowD[i]["MinTarget"].ToString(), out double Min);
                            CreateDGV1(Title + "_红绿表格_" + rowD[i]["ControlOrder"].ToString() + "_折线图", 95 + a, 1, rowD[i]["RowTitle1"].ToString(), rowD[i]["RowTitle2"].ToString(), rowD[i]["ControlType"].ToString(), Convert.ToInt32(rowD[i]["ControlOrder"].ToString()), Title, Max, Min, rowD[i]["Indicator"].ToString(), rowD[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        else
                        {
                            CreateDGV1(Title + "_红绿表格_" + rowD[i]["ControlOrder"].ToString(), 95 + a, 1, rowD[i]["RowTitle1"].ToString(), rowD[i]["RowTitle2"].ToString(), rowD[i]["ControlType"].ToString(), Convert.ToInt32(rowD[i]["ControlOrder"].ToString()), Title, 0, 0, rowD[i]["Indicator"].ToString(), rowD[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        FrameType = "红绿表格";
                    }
                    else if (rowD[i]["ControlType"].ToString() == "文字表格")
                    {
                        if (FrameType == "折线图")
                        {
                            CreateDGV2(Title + "_文字表格_" + rowD[i]["ControlOrder"].ToString() + "_折线图", 95 + a, Convert.ToInt32(rowD[i]["RowType"].ToString()), rowD[i]["RowTitle1"].ToString(), rowD[i]["RowTitle2"].ToString(), rowD[i]["ControlType"].ToString(), Convert.ToInt32(rowD[i]["ControlOrder"].ToString()), Title, rowD[i]["Indicator"].ToString(), rowD[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        else
                        {
                            CreateDGV2(Title + "_文字表格_" + rowD[i]["ControlOrder"].ToString(), 95 + a, Convert.ToInt32(rowD[i]["RowType"].ToString()), rowD[i]["RowTitle1"].ToString(), rowD[i]["RowTitle2"].ToString(), rowD[i]["ControlType"].ToString(), Convert.ToInt32(rowD[i]["ControlOrder"].ToString()), Title, rowD[i]["Indicator"].ToString(), rowD[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        FrameType = "文字表格";
                    }
                    else if (rowD[i]["ControlType"].ToString() == "折线图")
                    {
                        if (LineChartType == 0)
                        {
                            CreateLineChart1(Title + "_折线图_" + rowD[i]["ControlOrder"].ToString(), 95 + a, rowD[i]["ControlType"].ToString(), Convert.ToInt32(rowD[i]["ControlOrder"].ToString()), Title, Convert.ToDouble(rowD[i]["MaxTarget"].ToString()), rowD[i]["Indicator"].ToString(), rowD[i]["Target/Limit"].ToString(), rowD[i]["ControlName"].ToString());
                            a += 170;
                            FrameType = "折线图";
                        }
                        else
                        {
                            CreateLineChart3(Title + "_折线图_" + rowD[i]["ControlOrder"].ToString(), 95 + a, rowD[i]["ControlType"].ToString(), Convert.ToInt32(rowD[i]["ControlOrder"].ToString()), Title, Convert.ToDouble(rowD[i]["MaxTarget"].ToString()), Convert.ToDouble(rowD[i]["MinTarget"].ToString()), rowD[i]["Indicator"].ToString(), rowD[i]["Target/Limit"].ToString(), rowD[i]["ControlName"].ToString());
                            a += 250;
                            FrameType = "折线图";
                        }
                    }
                    else if (rowD[i]["ControlType"].ToString() == "文本表格")
                    {
                        if (FrameType == "红绿表格")
                        {
                            CreateDGV4(Title + "_文本表格_" + rowD[i]["ControlOrder"].ToString(), 95 + a, rowD[i]["ControlType"].ToString(), Convert.ToInt32(rowD[i]["ControlOrder"].ToString()), Title, Convert.ToInt32(rowD[i]["RowType"].ToString()));
                            a += 120;
                        }
                        else
                        {
                            CreateDGV4(Title + "_文本表格_" + rowD[i]["ControlOrder"].ToString(), 95 + a, rowD[i]["ControlType"].ToString(), Convert.ToInt32(rowD[i]["ControlOrder"].ToString()), Title, Convert.ToInt32(rowD[i]["RowType"].ToString()));
                            a += 120;

                        }
                        FrameType = "文本表格";
                    }
                }
            }
            if (rowC.Length > 0)
            {
                string Title = "C";
                Label myLabel = new Label();
                myLabel.Parent = panel1;
                myLabel.Name = Title;
                myLabel.Text = Title;
                myLabel.AutoSize = true;
                myLabel.ForeColor = Color.Black;
                myLabel.Font = new Font("Arial", 42);
                myLabel.Location = new Point(17, 80 + a);
                panel1.Controls.Add(myLabel);
                a += 10;
                for (int i = 0; i < rowC.Length; i++)
                {
                    if (rowC[i]["ControlType"].ToString() == "红绿表格")
                    {
                        if (FrameType == "折线图")
                        {
                            double.TryParse(rowC[i]["MaxTarget"].ToString(), out double Max);
                            double.TryParse(rowC[i]["MinTarget"].ToString(), out double Min);
                            CreateDGV1(Title + "_红绿表格_" + rowC[i]["ControlOrder"].ToString() + "_折线图", 95 + a, 1, rowC[i]["RowTitle1"].ToString(), rowC[i]["RowTitle2"].ToString(), rowC[i]["ControlType"].ToString(), Convert.ToInt32(rowC[i]["ControlOrder"].ToString()), Title, Max, Min, rowC[i]["Indicator"].ToString(), rowC[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        else
                        {
                            CreateDGV1(Title + "_红绿表格_" + rowC[i]["ControlOrder"].ToString(), 95 + a, 1, rowC[i]["RowTitle1"].ToString(), rowC[i]["RowTitle2"].ToString(), rowC[i]["ControlType"].ToString(), Convert.ToInt32(rowC[i]["ControlOrder"].ToString()), Title, 0, 0, rowC[i]["Indicator"].ToString(), rowC[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        FrameType = "红绿表格";
                    }
                    else if (rowC[i]["ControlType"].ToString() == "文字表格")
                    {
                        if (FrameType == "折线图")
                        {
                            CreateDGV2(Title + "_文字表格_" + rowC[i]["ControlOrder"].ToString() + "_折线图", 95 + a, Convert.ToInt32(rowC[i]["RowType"].ToString()), rowC[i]["RowTitle1"].ToString(), rowC[i]["RowTitle2"].ToString(), rowC[i]["ControlType"].ToString(), Convert.ToInt32(rowC[i]["ControlOrder"].ToString()), Title, rowC[i]["Indicator"].ToString(), rowC[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        else
                        {
                            CreateDGV2(Title + "_文字表格_" + rowC[i]["ControlOrder"].ToString(), 95 + a, Convert.ToInt32(rowC[i]["RowType"].ToString()), rowC[i]["RowTitle1"].ToString(), rowC[i]["RowTitle2"].ToString(), rowC[i]["ControlType"].ToString(), Convert.ToInt32(rowC[i]["ControlOrder"].ToString()), Title, rowC[i]["Indicator"].ToString(), rowC[i]["Target/Limit"].ToString());
                            a += 90;
                        }
                        FrameType = "文字表格";
                    }
                    else if (rowC[i]["ControlType"].ToString() == "折线图")
                    {
                        if (LineChartType == 0)
                        {
                            CreateLineChart1(Title + "_折线图_" + rowC[i]["ControlOrder"].ToString(), 95 + a, rowC[i]["ControlType"].ToString(), Convert.ToInt32(rowC[i]["ControlOrder"].ToString()), Title, Convert.ToDouble(rowC[i]["MaxTarget"].ToString()), rowC[i]["Indicator"].ToString(), rowC[i]["Target/Limit"].ToString(), rowC[i]["ControlName"].ToString());
                            a += 170;
                            FrameType = "折线图";
                        }
                        else
                        {
                            CreateLineChart3(Title + "_折线图_" + rowC[i]["ControlOrder"].ToString(), 95 + a, rowC[i]["ControlType"].ToString(), Convert.ToInt32(rowC[i]["ControlOrder"].ToString()), Title, Convert.ToDouble(rowC[i]["MaxTarget"].ToString()), Convert.ToDouble(rowC[i]["MinTarget"].ToString()), rowC[i]["Indicator"].ToString(), rowC[i]["Target/Limit"].ToString(), rowC[i]["ControlName"].ToString());
                            a += 250;
                            FrameType = "折线图";
                        }
                    }
                    else if (rowC[i]["ControlType"].ToString() == "文本表格")
                    {
                        if (FrameType == "红绿表格")
                        {
                            CreateDGV4(Title + "_文本表格_" + rowC[i]["ControlOrder"].ToString(), 95 + a, rowC[i]["ControlType"].ToString(), Convert.ToInt32(rowC[i]["ControlOrder"].ToString()), Title, Convert.ToInt32(rowC[i]["RowType"].ToString()));
                            a += 120;
                        }
                        else
                        {
                            CreateDGV4(Title + "_文本表格_" + rowC[i]["ControlOrder"].ToString(), 95 + a, rowC[i]["ControlType"].ToString(), Convert.ToInt32(rowC[i]["ControlOrder"].ToString()), Title, Convert.ToInt32(rowC[i]["RowType"].ToString()));
                            a += 120;

                        }
                        FrameType = "文本表格";
                    }
                }
            }
        }
        #region 红绿表格
        /// <summary>
        /// 创建红绿表格
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Top"></param>
        /// <param name="RowCount"></param>
        /// <param name="RowTitle1"></param>
        /// <param name="RowTitle2"></param>
        /// <param name="ControlType"></param>
        /// <param name="ControlOrder"></param>
        /// <param name="KPItype"></param>
        private void CreateDGV1(string Name, int Top, int RowCount, string RowTitle1, string RowTitle2, string ControlType, int ControlOrder, string KPItype, double Max, double Min, string Indicator, string TargetLimit)
        {
            DataGridView dgv = new DataGridView();
            dgv.Parent = panel1;
            dgv.Name = Name;
            dgv.Location = new System.Drawing.Point(90, Top);
            dgv.Size = new Size(957, 68);
            dgv.RowHeadersVisible = false;
            dgv.BackgroundColor = Color.White;
            dgv.BorderStyle = BorderStyle.None;
            dgv.AllowUserToAddRows = false;
            dgv.AllowUserToDeleteRows = false;
            dgv.DefaultCellStyle.Font = new Font("Arial", 6);
            dgv.ReadOnly = true;
            panel1.Controls.Add(dgv);
            if (Indicator != "")
            {
                Label label = new Label();
                label.Parent = panel1;
                label.Name = "Indicator: " + Indicator;
                label.Text = "Indicator: " + Indicator;
                label.Location = new System.Drawing.Point(90, Top - 20);
                label.AutoSize = true;
                label.Font = new Font("Arial", 9);
                panel1.Controls.Add(label);
            }
            if (TargetLimit != "")
            {
                Label label = new Label();
                label.Parent = panel1;
                label.Name = "Target/Limit: " + TargetLimit;
                label.Text = "Target/Limit: " + TargetLimit;
                label.Location = new System.Drawing.Point(730, Top - 20);
                label.AutoSize = false;
                label.Size = new Size(300, 15);
                label.TextAlign = ContentAlignment.TopRight;
                label.Font = new Font("Arial", 9);
                panel1.Controls.Add(label);
            }
            DataGridViewTextBoxColumn column1 = new DataGridViewTextBoxColumn();
            column1.Name = "Day";
            column1.HeaderText = "Day";
            column1.Width = 40;
            dgv.Columns.Add(column1);
            for (int i = 1; i < 32; i++)
            {
                DataGridViewTextBoxColumn column2 = new DataGridViewTextBoxColumn();
                column2.Name = i.ToString();
                column2.HeaderText = i.ToString();
                column2.Width = dgv.Width / 32;
                dgv.Columns.Add(column2);
            }
            if (RowCount == 1)
            {
                dgv.Rows.Add(RowTitle1);
                dgv.Rows[0].Height = 25;
            }
            else if (RowCount == 2)
            {
                dgv.Rows.Add(RowTitle1);
                dgv.Rows.Add(RowTitle2);
                dgv.Rows[0].Height = 18;
                dgv.Rows[1].Height = 18;
            }
            dgv.CellClick += Dgv_CellClick1;
            LoadData_DGV1(ControlType, ControlOrder, KPItype, dgv, RowCount);
            if (FrameType == "折线图")
            {
                dgv.DefaultCellStyle.ForeColor = Color.Transparent;
                dgv.CellValueChanged += (sender, e) => Dgv_CellValueChanged(sender, e, Max, Min);
                DgvCellValueChanged(dgv, Max, Min);
                string[] b = dgv.Name.Split(new char[] { '_' });
                int ID = Convert.ToInt32(b[2]) - 1;
                if (LineChartType == 0)
                {
                    UpdateLineChart1(dgv, (LineChart)panel1.Controls[KPItype + "_折线图_" + ID]);
                }
                else
                {
                    UpdateLineChart3(dgv, (LineChart2)panel1.Controls[KPItype + "_折线图_" + ID]);
                }
            }
        }
        private void Dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e, double Max, double Min)
        {
            if (sender is DataGridView)
            {
                DataGridView dataGridView = (DataGridView)sender;
                DgvCellValueChanged(dataGridView, Max, Min);
            }
        }
        //用于记录折线图配的红绿表格，但是输入时输入的时文字，如果文字超过上限，或者低于下限需要变色
        private void DgvCellValueChanged(DataGridView Dgv, double Max, double Min)
        {
            for (int i = 1; i < Dgv.Columns.Count; i++)
            {
                if (Dgv.Rows[0].Cells[i].Value != null)
                {
                    double.TryParse(Dgv.Rows[0].Cells[i].Value.ToString(), out double val);
                    if (val != 0)
                    {
                        if (Min != 0 && Max == 0)
                        {
                            //只有最小值时
                            if (val < Min)
                            {
                                Dgv.Rows[0].Cells[i].Style.BackColor = Color.Red;
                            }
                            else
                            {
                                Dgv.Rows[0].Cells[i].Style.BackColor = Color.Lime;
                            }
                        }
                        else if (Min == 0 && Max != 0)
                        {
                            if (val > Max)
                            {
                                Dgv.Rows[0].Cells[i].Style.BackColor = Color.Red;
                            }
                            else
                            {
                                Dgv.Rows[0].Cells[i].Style.BackColor = Color.Lime;
                            }
                        }
                        else if (Min != 0 && Max != 0)
                        {
                            if (val < Min || val > Max)
                            {
                                Dgv.Rows[0].Cells[i].Style.BackColor = Color.Red;
                            }
                            else
                            {
                                Dgv.Rows[0].Cells[i].Style.BackColor = Color.Lime;
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 加载红绿表格数据
        /// </summary>
        /// <param name="ControlType"></param>
        /// <param name="ControlOrder"></param>
        /// <param name="KPItype"></param>
        /// <param name="Dgv"></param>
        public void LoadData_DGV1(string ControlType, int ControlOrder, string KPItype, DataGridView Dgv, int RowCount)
        {
            string Condition = "FactoryID = " + FactoryID + " and DepartmentID=" + DepartmentID + " and ControlType='" + ControlType + "' and ControlOrder=" + ControlOrder + " and KPItype='" + KPItype + "' and RowType=" + RowCount + " and Year=" + Year + " and Month=" + Month;
            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "Data_DataGridView", Condition);
            if (dt1.Rows.Count > 0)
            {
                if (FrameType == "折线图")
                {
                    for (int i = 10; i < dt1.Columns.Count; i++)
                    {
                        Dgv.Rows[0].Cells[i - 9].Value = dt1.Rows[0][i].ToString();
                    }
                }
                else
                {
                    for (int i = 10; i < dt1.Columns.Count; i++)
                    {
                        if (dt1.Rows[0][i].ToString() == "1")
                        {
                            Dgv.Rows[0].Cells[i - 9].Style.BackColor = Color.Lime;
                        }
                        else if (dt1.Rows[0][i].ToString() == "2")
                        {
                            Dgv.Rows[0].Cells[i - 9].Style.BackColor = Color.Red;
                        }
                        else if (dt1.Rows[0][i].ToString() == "3")
                        {
                            Dgv.Rows[0].Cells[i - 9].Style.BackColor = Color.Gray;
                        }
                    }
                }
            }
            else
            {
                SQL.Insert(SystemInfo.SQL, "Data_DataGridView", "FactoryID,DepartmentID,Level,ControlType,RowType,ControlOrder,KPItype,Year,Month", $"{FactoryID},{DepartmentID},{Level},'{ControlType}',{1},{ControlOrder},'{KPItype}',{Year},{Month}");
                //2024.11.07增加打开红绿表格显示附件文本的
                if (ControlType == "红绿表格")
                {
                    SQL.Insert(SystemInfo.SQL, "Data_DataGridView", "FactoryID,DepartmentID,Level,ControlType,RowType,ControlOrder,KPItype,Year,Month", $"{FactoryID},{DepartmentID},{Level},'{ControlType}_comment',{1},{ControlOrder},'{KPItype}',{Year},{Month}");
                }
            }
            //2024.11.07增加打开红绿表格显示附件文本的，判断又没有*_comment，没有就创建
            string Condition2 = "FactoryID=" + FactoryID + "and DepartmentID=" + DepartmentID + "and ControlType='" + ControlType + "_comment' and ControlOrder=" + ControlOrder + "and KPItype='" + KPItype + "'and RowType=" + RowCount + " and Year=" + Year + " and Month=" + Month;
            DataTable dt2 = SQL.ReturnDataTable3(SystemInfo.SQL, "Data_DataGridView", Condition2);
            if (dt2.Rows.Count == 0)
            {
                if (ControlType == "红绿表格")
                {
                    SQL.Insert(SystemInfo.SQL, "Data_DataGridView", "FactoryID,DepartmentID,Level,ControlType,RowType,ControlOrder,KPItype,Year,Month", $"{FactoryID},{DepartmentID},{Level},'{ControlType}_comment',{1},{ControlOrder},'{KPItype}',{Year},{Month}");
                }
            }
        }
        /// <summary>
        /// 红绿表格的点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Dgv_CellClick1(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show($"Now is {dtp_Date.Value.ToString()}");
            if (e.ColumnIndex > 0 && e.ColumnIndex <= DateTime.Now.Day || e.ColumnIndex > 0 && DateTime.Now.Day == 1 || Convert.ToInt32(dtp_Date.Value.ToString("yyyyMM")) < Convert.ToInt32(DateTime.Now.ToString("yyyyMM")))
            {
                Control clickedControl = sender as Control;
                if (clickedControl != null)
                {
                    string[] b = clickedControl.Name.Split(new char[] { '_' });
                    if (sender is DataGridView)
                    {
                        //2024.11.07增加打开红绿表格显示附件文本的
                        string comment = "";
                        string Condition1 = "Year=" + Year + " and Month=" + Month + " and FactoryID=" + SystemInfo.readFactory + " and DepartmentID =" + DepartmentID + " and ControlOrder=" + b[2] + " and KPItype='" + b[0] + "' and ControlType='" + b[1] + "_comment'";
                        DataTable dataTable = SQL.ReturnDataTable3(SystemInfo.SQL, "Data_DataGridView", Condition1);
                        if (dataTable.Rows.Count > 0)
                        {
                            if (dataTable.Rows[0][e.ColumnIndex + 9].ToString() != "0")
                            {
                                comment = dataTable.Rows[0][e.ColumnIndex + 9].ToString();
                            }
                        }
                        DataGridView dataGridView = (DataGridView)sender;
                        if (b.Length == 4)
                        {
                            if (b[3] == "折线图")
                            {
                                Form_DgvInput frm = new Form_DgvInput();
                                frm.ControlType = b[1];
                                frm.ControlOrder = Convert.ToInt32(b[2]);
                                frm.KPItype = b[0];
                                frm.Day = e.ColumnIndex;
                                frm.FactoryID = FactoryID;
                                frm.DepartmentID = DepartmentID;
                                frm.Level = Level;
                                frm.Year = Year;
                                frm.Month = Month;
                                frm.label1.Text = dataGridView.Rows[0].Cells[0].Value.ToString();
                                frm.RowCount = dataGridView.Rows.Count;
                                frm.label2.Visible = false;
                                frm.txt_Actual.Visible = false;
                                frm.txt_Plan.Text = dataGridView.Rows[0].Cells[e.ColumnIndex].Value.ToString();
                                frm.dgv = dataGridView;
                                frm.tp = 1;
                                frm.ShowDialog();
                                if (b[3] == "折线图")
                                {
                                    int ID = Convert.ToInt32(b[2]) - 1;
                                    if (LineChartType == 0)
                                    {
                                        UpdateLineChart1(dataGridView, (LineChart)panel1.Controls[b[0] + "_折线图_" + ID]);
                                    }
                                    else
                                    {
                                        UpdateLineChart3(dataGridView, (LineChart2)panel1.Controls[b[0] + "_折线图_" + ID]);
                                    }
                                }
                                SetSQDC();
                            }
                        }
                        else
                        {
                            Form_GRinputForm frm = new Form_GRinputForm();
                            frm.type = clickedControl.Name;
                            frm.ControlType = b[1];
                            frm.ControlOrder = Convert.ToInt32(b[2]);
                            frm.KPItype = b[0];
                            frm.Day = e.ColumnIndex;
                            frm.FactoryID = FactoryID;
                            frm.DepartmentID = DepartmentID;
                            frm.Level = Level;
                            frm.Year = Year;
                            frm.Month = Month;
                            if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor == Color.Lime)
                            {
                                frm.RedGreen = 1;
                            }
                            else if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor == Color.Red)
                            {
                                frm.RedGreen = 2;
                            }
                            else if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor == Color.Gray)
                            {
                                frm.RedGreen = 3;
                            }
                            frm.txt_Comment.Text = comment;
                            //文本表格数据带入                         
                            string Condition = "FactoryID=" + FactoryID + "and DepartmentID=" + DepartmentID + "and ControlType='文本表格'and ControlOrder=" + (Convert.ToInt32(b[2]) + 1) + "and KPItype='" + b[0] + "'and RowType=1 and Year=" + Year + " and Month=" + Month;
                            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "Data_DataGridView", Condition);
                            if (dt1.Rows.Count > 0)
                            {
                                frm.txt_Comment.Text = dt1.Rows[0][e.ColumnIndex + 9].ToString();
                            }
                            frm.dgv = dataGridView;
                            frm.ShowDialog();
                            SetSQDC();
                        }
                    }
                }
            }

        }
        #endregion
        #region 文本表格
        /// <summary>
        /// 创建文字表格
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Top"></param>
        /// <param name="RowCount"></param>
        /// <param name="RowTitle1"></param>
        /// <param name="RowTitle2"></param>
        /// <param name="ControlType"></param>
        /// <param name="ControlOrder"></param>
        /// <param name="KPItype"></param>
        private void CreateDGV2(string Name, int Top, int RowCount, string RowTitle1, string RowTitle2, string ControlType, int ControlOrder, string KPItype, string Indicator, string TargetLimit)
        {
            DataGridView dgv = new DataGridView();
            dgv.Parent = panel1;
            dgv.Name = Name;
            dgv.Location = new System.Drawing.Point(90, Top);
            dgv.Size = new Size(957, 68);
            dgv.RowHeadersVisible = false;
            dgv.BackgroundColor = Color.White;
            dgv.BorderStyle = BorderStyle.None;
            dgv.AllowUserToAddRows = false;
            dgv.AllowUserToDeleteRows = false;
            dgv.DefaultCellStyle.Font = new Font("Arial", 6);
            dgv.ReadOnly = true;
            panel1.Controls.Add(dgv);
            if (Indicator != "")
            {
                Label label = new Label();
                label.Parent = panel1;
                label.Name = "Indicator: " + Indicator;
                label.Text = "Indicator: " + Indicator;
                label.Location = new System.Drawing.Point(90, Top - 20);
                label.AutoSize = true;
                label.Font = new Font("Arial", 9);
                panel1.Controls.Add(label);
            }
            if (TargetLimit != "")
            {
                Label label = new Label();
                label.Parent = panel1;
                label.Name = "Target/Limit: " + TargetLimit;
                label.Text = "Target/Limit: " + TargetLimit;
                label.Location = new System.Drawing.Point(730, Top - 20);
                label.AutoSize = false;
                label.Size = new Size(300, 15);
                label.TextAlign = ContentAlignment.TopRight;
                label.Font = new Font("Arial", 9);
                panel1.Controls.Add(label);
            }
            DataGridViewTextBoxColumn column1 = new DataGridViewTextBoxColumn();
            column1.Name = "Day";
            column1.HeaderText = "Day";
            column1.Width = 40;
            dgv.Columns.Add(column1);
            for (int i = 1; i < 32; i++)
            {
                DataGridViewTextBoxColumn column2 = new DataGridViewTextBoxColumn();
                column2.Name = i.ToString();
                column2.HeaderText = i.ToString();
                column2.Width = dgv.Width / 32;
                dgv.Columns.Add(column2);
            }
            if (RowCount == 1)
            {
                dgv.Rows.Add(RowTitle1, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
                dgv.Rows[0].Height = 25;
            }
            else if (RowCount == 2)
            {
                dgv.Rows.Add(RowTitle1, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
                dgv.Rows.Add(RowTitle2, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
                dgv.Rows[0].Height = 18;
                dgv.Rows[1].Height = 18;
            }
            dgv.CellClick += Dgv_CellClick2;
            LoadData_DGV2(ControlType, ControlOrder, KPItype, dgv, RowCount);
            if (FrameType == "折线图")
            {
                string[] b = dgv.Name.Split(new char[] { '_' });
                int ID = Convert.ToInt32(b[2]) - 1;
                if (LineChartType == 0)
                {
                    UpdateLineChart1(dgv, (LineChart)panel1.Controls[KPItype + "_折线图_" + ID]);
                }
                else
                {
                    UpdateLineChart3(dgv, (LineChart2)panel1.Controls[KPItype + "_折线图_" + ID]);
                }
            }
        }
        /// <summary>
        /// 加载文字表格数据
        /// </summary>
        /// <param name="ControlType"></param>
        /// <param name="ControlOrder"></param>
        /// <param name="KPItype"></param>
        /// <param name="Dgv"></param>
        /// <param name="RowCount"></param>
        public void LoadData_DGV2(string ControlType, int ControlOrder, string KPItype, DataGridView Dgv, int RowCount)
        {
            string Condition = "FactoryID=" + FactoryID + "and DepartmentID=" + DepartmentID + "and ControlType='" + ControlType + "'and ControlOrder=" + ControlOrder + "and KPItype='" + KPItype + "' and Year=" + Year + " and Month=" + Month;
            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "Data_DataGridView", Condition);
            if (dt1.Rows.Count > 0)
            {
                for (int i = 10; i < dt1.Columns.Count; i++)
                {
                    Dgv.Rows[0].Cells[i - 9].Value = dt1.Rows[0][i].ToString();
                    if (RowCount == 2 && dt1.Rows.Count > 1)
                    {
                        Dgv.Rows[1].Cells[i - 9].Value = dt1.Rows[1][i].ToString();
                    }
                }
            }
            else
            {
                SQL.Insert(SystemInfo.SQL, "Data_DataGridView", "FactoryID,DepartmentID,Level,ControlType,ControlOrder,KPItype,RowType,Year,Month", $"{FactoryID},{DepartmentID},{Level},'{ControlType}',{ControlOrder},'{KPItype}',{1},{Year},{Month}");
                if (RowCount == 2)
                {
                    SQL.Insert(SystemInfo.SQL, "Data_DataGridView", "FactoryID,DepartmentID,Level,ControlType,ControlOrder,KPItype,RowType,Year,Month", $"{FactoryID},{DepartmentID},{Level},'{ControlType}',{ControlOrder},'{KPItype}',{2},{Year},{Month}");
                }
            }
        }
        /// <summary>
        /// 文字表格的点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Dgv_CellClick2(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > 0 && e.ColumnIndex <= DateTime.Now.Day || e.ColumnIndex > 0 && DateTime.Now.Day == 1 || Convert.ToInt32(dtp_Date.Value.ToString("yyyyMM")) < Convert.ToInt32(DateTime.Now.ToString("yyyyMM")))
            {
                Control clickedControl = sender as Control;
                if (clickedControl != null)
                {
                    Form_DgvInput frm = new Form_DgvInput();
                    frm.Row = e.RowIndex;
                    string[] b = clickedControl.Name.Split(new char[] { '_' });
                    frm.ControlType = b[1];
                    frm.ControlOrder = Convert.ToInt32(b[2]);
                    frm.KPItype = b[0];
                    frm.Day = e.ColumnIndex;
                    frm.FactoryID = FactoryID;
                    frm.DepartmentID = DepartmentID;
                    frm.Level = Level;
                    frm.Year = Year;
                    frm.Month = Month;
                    if (sender is DataGridView)
                    {
                        DataGridView dataGridView = (DataGridView)sender;
                        frm.dgv = dataGridView;
                        frm.label1.Text = dataGridView.Rows[0].Cells[0].Value.ToString();
                        frm.RowCount = dataGridView.Rows.Count;
                        if (dataGridView.Rows.Count == 1)
                        {
                            frm.label2.Visible = false;
                            frm.txt_Actual.Visible = false;
                            frm.txt_Plan.Text = dataGridView.Rows[0].Cells[e.ColumnIndex].Value.ToString();
                        }
                        else
                        {
                            frm.label2.Text = dataGridView.Rows[1].Cells[0].Value.ToString();
                            frm.txt_Plan.Text = dataGridView.Rows[0].Cells[e.ColumnIndex].Value.ToString();
                            frm.txt_Actual.Text = dataGridView.Rows[1].Cells[e.ColumnIndex].Value.ToString();
                        }
                        frm.ShowDialog();
                        if (b.Length == 4)
                        {
                            if (b[3] == "折线图")
                            {
                                int ID = Convert.ToInt32(b[2]) - 1;
                                if (LineChartType == 0)
                                {
                                    UpdateLineChart1(dataGridView, (LineChart)panel1.Controls[b[0] + "_折线图_" + ID]);
                                }
                                else
                                {
                                    UpdateLineChart3(dataGridView, (LineChart2)panel1.Controls[b[0] + "_折线图_" + ID]);
                                }
                            }
                        }
                        SetSQDC();
                    }
                }
            }

        }
        #endregion     
        #region 折线图2
        private void CreateLineChart1(string Name, int Top, string ControlType, int ControlOrder, string KPItype, double Target, string Indicator, string TargetLimit, string ControlName)
        {
            LineChart lineChart = new LineChart();
            lineChart.Parent = panel1;
            lineChart.Name = Name;
            lineChart.Location = new System.Drawing.Point(90, Top);
            lineChart.Size = new Size(1000, 150);
            lineChart.AxisXFont = new Font("宋体", 7);
            lineChart.AxisYFont = new Font("宋体", 8);
            lineChart.SeriesTextVisible = false;
            lineChart.TargetVisible = true;
            lineChart.TargetValue = Target;
            lineChart.ValueShownAsLabel = true;
            lineChart.LineColor = Color.Blue;
            lineChart.LineColor2 = Color.LimeGreen;
            panel1.Controls.Add(lineChart);
            if (Indicator != "")
            {
                Label label = new Label();
                label.Parent = panel1;
                label.Name = "Indicator:" + Indicator;
                label.Text = "Indicator:" + Indicator;
                label.Location = new System.Drawing.Point(90, Top - 20);
                label.AutoSize = true;
                label.Font = new Font("Microsoft Sans Serif", 9);
                panel1.Controls.Add(label);
            }
            if (TargetLimit != "")
            {
                Label label = new Label();
                label.Parent = panel1;
                label.Name = "Target/Limit: " + TargetLimit;
                label.Text = "Target/Limit: " + TargetLimit;
                label.Location = new System.Drawing.Point(730, Top - 20);
                label.AutoSize = false;
                label.Size = new Size(300, 15);
                label.TextAlign = ContentAlignment.TopRight;
                label.Font = new Font("Microsoft Sans Serif", 9);
                panel1.Controls.Add(label);
            }
            if (ControlName != "")
            {
                string[] b = ControlName.Split(new char[] { '_' });
                Label label = new Label();
                label.Parent = panel1;
                label.Name = b[0];
                label.Text = b[0];
                label.Location = new System.Drawing.Point(410, Top - 20);
                label.AutoSize = false;
                label.Size = new Size(300, 15);
                label.TextAlign = ContentAlignment.TopCenter;
                label.Font = new Font("Microsoft Sans Serif", 9);
                panel1.Controls.Add(label);
            }
        }
        private void UpdateLineChart1(DataGridView Dgv, LineChart lineChart)
        {
            double[] doubles = new double[31];
            for (int i = 1; i < Dgv.Columns.Count; i++)
            {
                if (Dgv.Rows.Count > 1)
                {
                    double.TryParse(Dgv.Rows[0].Cells[i].Value.ToString(), out double firstRowValue);
                    double.TryParse(Dgv.Rows[1].Cells[i].Value.ToString(), out double secondRowValue);

                    if (firstRowValue != 0) // 避免除以0的情况
                    {
                        doubles[i - 1] = Math.Round(secondRowValue / firstRowValue, 2);
                    }
                    else
                    {
                        doubles[i - 1] = 0; // 或者其他适当的处理方式
                    }
                }
                else if (Dgv.Rows.Count == 1)
                {
                    double.TryParse(Dgv.Rows[0].Cells[i].Value.ToString(), out double firstRowValue);
                    doubles[i - 1] = firstRowValue;
                }
            }
            lineChart.XaxisValues = doubles;
            if ((int)doubles.Max() + 1 > lineChart.YaxisMinmum)
            {
                lineChart.YaxisMaximum = (int)doubles.Max() + 1;
            }
        }
        #endregion
        #region  折线图3
        private void CreateLineChart3(string Name, int Top, string ControlType, int ControlOrder, string KPItype, double MaxTarget, double MinTarget, string Indicator, string TargetLimit, string ControlName)
        {
            LineChart2 lineChart = new LineChart2();
            lineChart.Parent = panel1;
            lineChart.Name = Name;
            lineChart.Location = new System.Drawing.Point(90, Top - 20);
            lineChart.Size = new Size(980, 250);
            lineChart.XValues = new List<double>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };
            lineChart.YValues = new List<double>() { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            if (Indicator != "")
            {
                lineChart.ChartLabel1 = Indicator;
            }
            string[] b = ControlName.Split(new char[] { '_' });
            lineChart.ChartLabel2 = b[0];
            if (TargetLimit != "")
            {
                lineChart.ChartLabel3 = TargetLimit;
            }
            lineChart.DataLabelFont = new Font("Arial", 7.5f);
            lineChart.DataLabelDecimalPlaces = 1;
            if (MaxTarget > 0)
            {
                lineChart.LimitDecimalPlaces = 2;
                lineChart.EnableUpperLimit = true;
                lineChart.UpperLimit = MaxTarget;
            }
            if (MinTarget > 0)
            {
                lineChart.LimitDecimalPlaces = 2;

                lineChart.EnableLowerLimit = true;
                lineChart.LowerLimit = MinTarget;
            }
            lineChart.ShowLimitLabels = true;
            lineChart.YDecimalPlaces = 1;
            lineChart.ShowLimitLabels = false;
            //lineChart.YAxisStartFromZero = false;
            panel1.Controls.Add(lineChart);
        }
        private void UpdateLineChart3(DataGridView Dgv, LineChart2 lineChart)
        {
            List<double> val = new List<double>();
            for (int i = 1; i < Dgv.Columns.Count; i++)
            {
                if (Dgv.Rows.Count > 1)
                {
                    double.TryParse(Dgv.Rows[0].Cells[i].Value.ToString(), out double firstRowValue);
                    double.TryParse(Dgv.Rows[1].Cells[i].Value.ToString(), out double secondRowValue);
                    double vv = 0;
                    if (firstRowValue != 0) // 避免除以0的情况
                    {
                        vv = Math.Round(secondRowValue * 100 / firstRowValue, 2);
                    }
                    else
                    {
                        vv = 0;
                    }
                    val.Add(vv);
                }
                else if (Dgv.Rows.Count == 1)
                {
                    if (Dgv.Rows[0].Cells[i].Value != null)
                    {
                        double.TryParse(Dgv.Rows[0].Cells[i].Value.ToString(), out double firstRowValue);
                        val.Add(firstRowValue);
                    }
                }
            }
            lineChart.YValues = val;
        }
        #endregion
        #region 文本表格
        private void CreateDGV4(string Name, int Top, string ControlType, int ControlOrder, string KPItype, int RowCount)
        {
            DataGridView dgv = new DataGridView();
            dgv.Parent = panel1;
            dgv.Name = Name;
            dgv.Location = new System.Drawing.Point(90, Top - 10);
            dgv.Size = new Size(957, 95);
            dgv.RowHeadersVisible = false;
            dgv.BackgroundColor = Color.White;
            dgv.BorderStyle = BorderStyle.None;
            dgv.AllowUserToAddRows = false;
            dgv.AllowUserToDeleteRows = false;
            dgv.ReadOnly = true;
            dgv.ColumnHeadersVisible = false;
            dgv.RowTemplate.Height = 30;
            panel1.Controls.Add(dgv);
            DataGridViewTextBoxColumn column1 = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn column2 = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn column3 = new DataGridViewTextBoxColumn();
            column1.Width = dgv.Width / 3 - 1;
            column2.Width = dgv.Width / 3 - 1;
            column3.Width = dgv.Width / 3 - 1;
            dgv.Columns.Add(column1);
            dgv.Columns.Add(column2);
            dgv.Columns.Add(column3);
            dgv.Rows.Add();
            dgv.Rows.Add();
            dgv.Rows.Add();
            //dgv.CellClick += Dgv_CellClick1;
            //LoadData_DGV1(ControlType, ControlOrder, KPItype, dgv);
            LoadData_DGV4(ControlType, ControlOrder, KPItype, dgv, 1);
        }
        public void LoadData_DGV4(string ControlType, int ControlOrder, string KPItype, DataGridView Dgv, int RowCount)
        {
            Dgv.Rows[0].Cells[0].Value = "";
            Dgv.Rows[0].Cells[1].Value = "";
            Dgv.Rows[0].Cells[2].Value = "";
            Dgv.Rows[1].Cells[0].Value = "";
            Dgv.Rows[1].Cells[1].Value = "";
            Dgv.Rows[1].Cells[2].Value = "";
            Dgv.Rows[2].Cells[0].Value = "";
            Dgv.Rows[2].Cells[1].Value = "";
            Dgv.Rows[2].Cells[2].Value = "";
            string Condition = "FactoryID=" + FactoryID + "and DepartmentID=" + DepartmentID + "and ControlType='" + ControlType + "'and ControlOrder=" + ControlOrder + "and KPItype='" + KPItype + "'and RowType=" + RowCount + " and Year=" + Year + " and Month=" + Month;
            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "Data_DataGridView", Condition);
            if (dt1.Rows.Count > 0)
            {
                int ad = 0;
                for (int i = 10; i < dt1.Columns.Count; i++)
                {
                    if (dt1.Rows[0][i].ToString() != "" && dt1.Rows[0][i].ToString() != "0")
                    {
                        if (ad < 3)
                        {
                            Dgv.Rows[0].Cells[ad].Value = DateTime.Now.Year + "/" + Month + "/" + (i - 9) + "：" + dt1.Rows[0][i].ToString();
                        }
                        else if (ad > 2 && ad < 6)
                        {
                            Dgv.Rows[1].Cells[ad - 3].Value = DateTime.Now.Year + "/" + Month + "/" + (i - 9) + "：" + dt1.Rows[0][i].ToString();
                        }
                        else if (ad > 5 && ad < 9)
                        {
                            Dgv.Rows[2].Cells[ad - 6].Value = DateTime.Now.Year + "/" + Month + "/" + (i - 9) + "：" + dt1.Rows[0][i].ToString();
                        }
                        ad++;
                    }
                }
            }
            else
            {
                SQL.Insert(SystemInfo.SQL, "Data_DataGridView", "FactoryID,DepartmentID,Level,ControlType,RowType,ControlOrder,KPItype,Year,Month", $"{FactoryID},{DepartmentID},{Level},'{ControlType}',{1},{ControlOrder},'{KPItype}',{Year},{Month}");
            }
        }
        #endregion
        #region  加载右侧部门  Top 3 Issues
        private int DateAdd = 0;
        private void btnPre_Click(object sender, EventArgs e)
        {
            DateAdd -= 1;
            lblDate.Text = DateTime.Now.AddDays(DateAdd).Date.ToString("yyyy/MM/dd");
            LoadTopIssues(DateAdd);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            DateAdd += 1;
            lblDate.Text = DateTime.Now.AddDays(DateAdd).Date.ToString("yyyy/MM/dd");
            LoadTopIssues(DateAdd);
        }
        private void LoadTopIssues(int add)
        { 
            #region   清理
            lab_issue1.Text = "";
            lab_issue2.Text = "";
            lab_issue3.Text = "";
            lab_act11.Text = "";
            lab_act12.Text = "";
            lab_act13.Text = "";
            lab_act21.Text = "";
            lab_act22.Text = "";
            lab_act23.Text = "";
            lab_act31.Text = "";
            lab_act32.Text = "";
            lab_act33.Text = "";
            lab_res11.Text = "";
            lab_res12.Text = "";
            lab_res13.Text = "";
            lab_res21.Text = "";
            lab_res22.Text = "";
            lab_res23.Text = "";
            lab_res31.Text = "";
            lab_res32.Text = "";
            lab_res33.Text = "";
            #endregion
            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "Top_Issue_Tag", "DepartmentID=" + DepartmentID + " and Date='" + DateTime.Now.AddDays(add).Date.ToString("yyyy/MM/dd") + "'");
            lblDate.Text = DateTime.Now.AddDays(add).Date.ToString("yyyy/MM/dd");
            if (DateAdd == 0)
            {
                btnNext.Enabled = false;
            }
            else
            {
                btnNext.Enabled = true;
            }

            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                string ID1 = dt1.Rows[i]["Top1ID"].ToString();
                string ID2 = dt1.Rows[i]["Top2ID"].ToString();
                string ID3 = dt1.Rows[i]["Top3ID"].ToString();
                if (ID1 != "0")
                {
                    lb_Top1ID.Text = ID1;
                }
                else
                {
                    lb_Top1ID.Text = "0";
                }
                if (ID2 != "0")
                {
                    lb_Top2ID.Text = ID2;
                }
                else
                {
                    lb_Top2ID.Text = "0";
                }
                if (ID3 != "0")
                {
                    lb_Top3ID.Text = ID3;
                }
                else
                {
                    lb_Top3ID.Text = "0";
                }
            }
        }
        private void lb_Top1ID_TextChanged(object sender, EventArgs e)
        {
            if (Convert.ToInt32(lb_Top1ID.Text) > 0)
            {
                DataTable dt1 = SQL.ReturnDataTableAsc(SystemInfo.SQL, "View_Todo", "TopicID=" + lb_Top1ID.Text);
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    if (i == 0)
                    {
                        label1.Text = dt1.Rows[i][0].ToString();
                        lab_issue1.Text = dt1.Rows[i][3].ToString();
                        lab_act11.Text = dt1.Rows[i][5].ToString();
                        lab_res11.Text = dt1.Rows[i][6].ToString();
                        if (Convert.ToBoolean(dt1.Rows[i][24].ToString()) == true)
                        {
                            pb_t1.Visible = true;
                            pb_f1.Visible = false;
                        }
                        else
                        {
                            pb_t1.Visible = false;
                            pb_f1.Visible = true;
                        }
                    }
                    else if (i == 1)
                    {
                        label2.Text = dt1.Rows[i][0].ToString();
                        lab_act12.Text = dt1.Rows[i][5].ToString();
                        lab_res12.Text = dt1.Rows[i][6].ToString();
                    }
                    else if (i == 2)
                    {
                        label3.Text = dt1.Rows[i][0].ToString();
                        lab_act13.Text = dt1.Rows[i][5].ToString();
                        lab_res13.Text = dt1.Rows[i][6].ToString();
                    }
                }
                if (dt1.Rows.Count == 0)
                {
                    DataTable dt2 = SQL.ReturnDataTable2(SystemInfo.SQL, "View_Topic", "ID=" + lb_Top1ID.Text);

                    if (dt2.Rows.Count > 0)
                    {
                        lab_issue1.Text = dt2.Rows[0]["TopicINFO"].ToString();
                    }

                }
            }
        }
        private void lb_Top2ID_TextChanged(object sender, EventArgs e)
        {
            if (Convert.ToInt32(lb_Top2ID.Text) > 0)
            {
                DataTable dt1 = SQL.ReturnDataTableAsc(SystemInfo.SQL, "View_Todo", "TopicID=" + lb_Top2ID.Text);
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    if (i == 0)
                    {
                        label4.Text = dt1.Rows[i][0].ToString();
                        lab_issue2.Text = dt1.Rows[i][3].ToString();
                        lab_act21.Text = dt1.Rows[i][5].ToString();
                        lab_res21.Text = dt1.Rows[i][6].ToString();
                        if (Convert.ToBoolean(dt1.Rows[i][24].ToString()) == true)
                        {
                            pb_t2.Visible = true;
                            pb_f2.Visible = false;
                        }
                        else
                        {
                            pb_t2.Visible = false;
                            pb_f2.Visible = true;
                        }
                    }
                    else if (i == 1)
                    {
                        label5.Text = dt1.Rows[i][0].ToString();
                        lab_act22.Text = dt1.Rows[i][5].ToString();
                        lab_res22.Text = dt1.Rows[i][6].ToString();
                    }
                    else if (i == 2)
                    {
                        label6.Text = dt1.Rows[i][0].ToString();
                        lab_act23.Text = dt1.Rows[i][5].ToString();
                        lab_res23.Text = dt1.Rows[i][6].ToString();
                    }
                }
                if (dt1.Rows.Count == 0)
                {
                    DataTable dt2 = SQL.ReturnDataTable2(SystemInfo.SQL, "View_Topic", "ID=" + lb_Top2ID.Text);

                    if (dt2.Rows.Count > 0)
                    {
                        lab_issue2.Text = dt2.Rows[0]["TopicINFO"].ToString();
                    }

                }
            }
        }
        private void lb_Top3ID_TextChanged(object sender, EventArgs e)
        {
            if (Convert.ToInt32(lb_Top3ID.Text) > 0)
            {
                DataTable dt1 = SQL.ReturnDataTableAsc(SystemInfo.SQL, "View_Todo", "TopicID=" + lb_Top3ID.Text);
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    if (i == 0)
                    {
                        label7.Text = dt1.Rows[i][0].ToString();
                        lab_issue3.Text = dt1.Rows[i][3].ToString();
                        lab_act31.Text = dt1.Rows[i][5].ToString();
                        lab_res31.Text = dt1.Rows[i][6].ToString();
                        if (Convert.ToBoolean(dt1.Rows[i][24].ToString()) == true)
                        {
                            pb_t3.Visible = true;
                            pb_f3.Visible = false;
                        }
                        else
                        {
                            pb_t3.Visible = false;
                            pb_f3.Visible = true;
                        }
                    }
                    else if (i == 1)
                    {
                        label8.Text = dt1.Rows[i][0].ToString();
                        lab_act32.Text = dt1.Rows[i][5].ToString();
                        lab_res32.Text = dt1.Rows[i][6].ToString();
                    }
                    else if (i == 2)
                    {
                        label9.Text = dt1.Rows[i][0].ToString();
                        lab_act33.Text = dt1.Rows[i][5].ToString();
                        lab_res33.Text = dt1.Rows[i][6].ToString();
                    }
                }
                if (dt1.Rows.Count == 0)
                {
                    DataTable dt2 = SQL.ReturnDataTable2(SystemInfo.SQL, "View_Topic", "ID=" + lb_Top3ID.Text);

                    if (dt2.Rows.Count > 0)
                    {
                        lab_issue3.Text = dt2.Rows[0]["TopicINFO"].ToString();
                    }

                }
            }
        }
        private void Label_Click(object sender, EventArgs e)
        {
            // 获取被点击的 UILabel
            UILabel clickedLabel = sender as UILabel;
            lbName = clickedLabel.Name;
            lbText = clickedLabel.Text;
            switch (lbName)
            {
                case "lab_act11":
                    TodoID = label1.Text; TopicID = lb_Top1ID.Text; topicStr = lab_issue1.Text; break;
                case "lab_act12":
                    TodoID = label2.Text; TopicID = lb_Top1ID.Text; topicStr = lab_issue1.Text; break;
                case "lab_act13":
                    TodoID = label3.Text; TopicID = lb_Top1ID.Text; topicStr = lab_issue1.Text; break;
                case "lab_act21":
                    TodoID = label4.Text; TopicID = lb_Top2ID.Text; topicStr = lab_issue2.Text; break;
                case "lab_act22":
                    TodoID = label5.Text; TopicID = lb_Top2ID.Text; topicStr = lab_issue2.Text; break;
                case "lab_act23":
                    TodoID = label6.Text; TopicID = lb_Top2ID.Text; topicStr = lab_issue2.Text; break;
                case "lab_act31":
                    TodoID = label7.Text; TopicID = lb_Top3ID.Text; topicStr = lab_issue3.Text; break;
                case "lab_act32":
                    TodoID = label8.Text; TopicID = lb_Top3ID.Text; topicStr = lab_issue3.Text; break;
                case "lab_act33":
                    TodoID = label9.Text; TopicID = lb_Top3ID.Text; topicStr = lab_issue3.Text; break;
            }
            // 遍历所有的 UILabel，设置背景色
            foreach (var label in labels)
            {
                if (label == clickedLabel)
                {
                    label.BackColor = System.Drawing.Color.LightSteelBlue;
                }
                else
                {
                    label.BackColor = System.Drawing.Color.White;
                }
            }
        }
        public static void SelectRequirement(string str1, string str2)
        {

            if (int.TryParse(TodoID, out int ID) == true)
            {
                if (SQL.Update(SystemInfo.SQL, "Todo", str1 + "=1", "ID=" + ID) == true)
                {
                    //MessageBox.Show("成功设置" + str2 + "！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("失败！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("失败！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btn_GoSeeMark_Click(object sender, EventArgs e)
        {

            if (publicFunc.GetUserScope(SystemInfo.userID, DepartmentID) == false)
            {
                XtraMessageBox.Show("You have no permission");
                return;
            }
            SelectRequirement("isGoSee", "Go & See");
            int res = SQL.Insert(SystemInfo.SQL, "GoSee", "Todo_ID,Now_Action,Create_time,Create_userID,Create_user,Del_tag,Done_tag", $"{TodoID},'{lbText}','{DateTime.Now}',{SystemInfo.userID},'{SystemInfo.userName}',{0},{0}");
            if (res > 0)
            {
                XtraMessageBox.Show("成功设置为 Go & See");


            }
        }
        public static void btn_PdcaMark_Click()
        {
            if (publicFunc.GetUserScope(SystemInfo.userID, DepartmentID) == false)
            {
                XtraMessageBox.Show("You have no permission");
                return;
            }
            SelectRequirement("isPDCA", "PDCA");
            DataTable dt = SQL.ReturnDataTableDist(SystemInfo.sys, "Codename", "Factories", $"ID = {SystemInfo.readFactory}");
            string code = dt.Rows[0]["Codename"].ToString();
            string datetime = DateTime.Now.ToString("yyMMdd");
            string condition = $"select count(*) as count from A3_List where FactoryID = {SystemInfo.readFactory} and DepartmentID = {DepartmentID}";
            DataTable dtCount = SQL.ReturnDataTable(SystemInfo.SQL, condition);
            int aa = (int)dtCount.Rows[0]["count"] + 1;
            string a3count;
            if (aa < 10)
            {
                a3count = $"0{aa}";
            }
            else
            {
                a3count = aa.ToString();
            }

            string A3code = code + datetime + a3count;

            string col = "FactoryID,DepartmentID,A3ReportNumber,A3ProblemStatement,Add_date";
            string value = $"{SystemInfo.readFactory},{SystemInfo.readparentDep},'{A3code}','{topicStr}','{DateTime.Now.Date}'";
            int res = SQL.InsertReturnID(SystemInfo.SQL, "A3_list", col, value);
            if (res > 0)
            {
                XtraMessageBox.Show("成功将问题录入A3 list");
            }
            //SQL.Insert(SystemInfo.SQL, "A3_List", "TodoID,ActionInfo,Create_time,Create_userID,Create_user,Del_tag", $"{TodoID},'{lbText}','{DateTime.Now}',{SystemInfo.userID},'{SystemInfo.userName}',{0}");
        }
        private void btn_ActionMark_Click(object sender, EventArgs e)
        {
            if (publicFunc.GetUserScope(SystemInfo.userID, DepartmentID) == false)
            {
                XtraMessageBox.Show("You have no permission");
                return;
            }
            SelectRequirement("isActionPlan", "Action Plan");
            SQL.Insert(SystemInfo.SQL, "ActionPlan", "Todo_ID,Now_Action,Create_time,Create_userID,Create_user,Del_tag,Done_tag", $"{TodoID},'{lbText}','{DateTime.Now}',{SystemInfo.userID},'{SystemInfo.userName}',{0},{0}");
        }
        private void btn_FeedBackMark_Click(object sender, EventArgs e)
        {
            if (publicFunc.GetUserScope(SystemInfo.userID, DepartmentID) == false)
            {
                XtraMessageBox.Show("You have no permission");
                return;
            }
            SelectRequirement("isFeedback", "Feedback");
            int i = SQL.Insert(SystemInfo.SQL, "Feedback", "Todo_ID,Now_Action,Create_time,Create_userID,Create_user,Del_tag,Done_tag", $"{TodoID},'{lbText}','{DateTime.Now}',{SystemInfo.userID},'{SystemInfo.userName}',{0},{0}");
            if (i > 0)
            {
                MessageBox.Show("添加成功");
            }
        
        
        }
        #endregion
        #region  加载右侧部门  Open item list
        private void LoadOpenItem()
        {
            dgv_actionplan.Rows.Clear();
            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "Top_Issue_Tag", "DepartmentID=" + DepartmentID + " and Date<'" + DateTime.Now.Date.ToString("yyyy/MM/dd") + "'");
            string Condition = "提出部门ID=" + DepartmentID + " and isConfirmation=0 and Del_tag=0 or 负责部门ID=" + DepartmentID + " and isConfirmation=0 and Del_tag=0";
            DataTable dt2 = SQL.ReturnDataTable2(SystemInfo.SQL, "View_Todo", Condition);
            dt1.DefaultView.Sort = "Date desc";
            dt1 = dt1.DefaultView.ToTable();
            List<string> IDlist = new List<string>();
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                for (int j = 2; j < 5; j++)
                {
                    if (dt1.Rows[i][j].ToString() != "0")
                    {
                        if (!IDlist.Contains(dt1.Rows[i][j].ToString()))
                        {
                            IDlist.Add(dt1.Rows[i][j].ToString());
                            //在Todo表中筛选出该TopicID的所有措施，并添加
                            DataRow[] dr1 = dt2.Select("TopicID=" + dt1.Rows[i][j].ToString());
                            for (int k = 0; k < dr1.Length; k++)
                            {
                                int ID = dgv_actionplan.Rows.Count + 1;
                                string Date = Convert.ToDateTime(dr1[k][1].ToString()).ToString("yyyy/MM/dd");
                                string Assigned_by = dr1[k][2].ToString();
                                string Topic = dr1[k][3].ToString();
                                string Root = dr1[k][4].ToString();
                                string Todo = dr1[k][5].ToString();
                                string ResDep = dr1[k][6].ToString();
                                string Resperson = dr1[k][7].ToString();
                                string Due_on = Convert.ToDateTime(dr1[k][8].ToString()).ToString("yyyy/MM/dd");
                                string Status = dr1[k][29].ToString();
                                string Comment = dr1[k][10].ToString();
                                Image image = null;
                                if (Status == "1")
                                {
                                    image = Properties.Resources.进度0;
                                }
                                else if (Status == "2")
                                {
                                    image = Properties.Resources.进度25;
                                }
                                else if (Status == "3")
                                {
                                    image = Properties.Resources.进度50;
                                }
                                else if (Status == "4")
                                {
                                    image = Properties.Resources.进度75;
                                }
                                else if (Status == "5")
                                {
                                    image = Properties.Resources.进度100;
                                }
                                dgv_actionplan.Rows.Add(ID, Date, Assigned_by, Topic, Root, Todo, ResDep, Resperson, Due_on, image, Comment);
                            }
                        }
                    }
                }
            }

        }
        #endregion
        private void SetSQDC()
        {
            int S = 0;
            int Q = 0;
            int D = 0;
            int C = 0;
            int Day = DateTime.Now.AddDays(-1).Day;
            string Condition1 = "FactoryID=" + FactoryID + "and DepartmentID=" + DepartmentID + "and Level=" + Level + " and Del_tag=0";
            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "Interface", Condition1);
            string Condition2 = "FactoryID=" + FactoryID + "and DepartmentID=" + DepartmentID + "and Level=" + Level + " and Year=" + Year + " and Month=" + Month;
            DataTable dt2 = SQL.ReturnDataTable3(SystemInfo.SQL, "Data_DataGridView", Condition2);
            dt1.DefaultView.Sort = "ControlOrder asc";
            dt1 = dt1.DefaultView.ToTable();
            DataRow[] drS = dt1.Select("KPItype='S'");
            DataRow[] drQ = dt1.Select("KPItype='Q' and ControlType='红绿表格'");
            DataRow[] drD = dt1.Select("KPItype='D'");
            DataRow[] drC = dt1.Select("KPItype='C'");
            //判断S且只有红绿
            for (int i = 0; i < drS.Length; i++)
            {
                if (drS[i][6].ToString() == "红绿表格")
                {
                    DataRow[] drData = dt2.Select("ControlOrder=" + drS[i][3].ToString());
                    if (drData.Length > 0)
                    {
                        if (drData[0][Day + 9].ToString() == "2")
                        {
                            S++;
                        }
                    }
                }
            }
            //判断Q且只有红绿,如果只有一行时就读第一行 ，如果有两行时只用第二个红绿（外部投诉）
            //2025-02-25 更新策略有红则红

            if (drQ.Length == 1)
            {
                DataRow[] drData = dt2.Select("ControlOrder=" + drQ[0][3].ToString());
                if (drData.Length > 0)
                {
                    if (drData[0][Day + 9].ToString() == "2")
                    {
                        Q++;
                    }
                }
            }
            else if (drQ.Length == 2)
            {
                DataRow[] drData1 = dt2.Select("ControlOrder=" + drQ[0][3].ToString());
                DataRow[] drData2 = dt2.Select("ControlOrder=" + drQ[1][3].ToString());
                if (drData1.Length > 0)
                {
                    if (drData1[0][Day + 9].ToString() == "2")
                    {
                        Q++;
                    }
                }
                if (drData2.Length > 0)
                {
                    if (drData2[0][Day + 9].ToString() == "2")
                    {
                        Q++;
                    }
                }
            }
            else
            {
                for (int i = 0; i < drQ.Length; i++)
                {
                    DataRow[] drDatai = dt2.Select($"ControlOrder = {drQ[i][3].ToString()}");

                    if (drDatai.Length > 0)
                    {
                        if (drDatai[0][Day+9].ToString() == "2")
                        {
                            Q++;
                        }
                    }
                }
            }

                //判断D，有双行文字表格，有单行文字表格，有红绿表格
                for (int i = 0; i < drD.Length; i++)
                {
                    if (drD[i][6].ToString() == "红绿表格")
                    {
                        DataRow[] drData = dt2.Select("ControlOrder=" + drD[i][3].ToString());
                        if (drData.Length > 0)
                        {  //红绿表格需要进行判断，如果是跟折线图搭配的红绿属于特殊的，它的输入内容是文字的，根据最大最小值判断红绿                
                            if (drD[i]["MaxTarget"].ToString() != "" && drD[i]["MinTarget"].ToString() != "")
                            {
                                double Max = Convert.ToDouble(drD[i]["MaxTarget"].ToString());
                                double Min = Convert.ToDouble(drD[i]["MinTarget"].ToString());
                                if (drData[0][Day + 9].ToString() != "")
                                {
                                    if (Min != 0 && Max == 0)
                                    {
                                        //只有最小值时
                                        if (Convert.ToDouble(drData[0][Day + 9].ToString()) < Min)
                                        {
                                            D++;
                                        }
                                    }
                                    else if (Min == 0 && Max != 0)
                                    {
                                        if (Convert.ToDouble(drData[0][Day + 9].ToString()) > Max)
                                        {
                                            D++;
                                        }
                                    }
                                    else if (Min != 0 && Max != 0)
                                    {
                                        if (Convert.ToDouble(drData[0][Day + 9].ToString()) < Min || Convert.ToDouble(drData[0][Day + 9].ToString()) > Max)
                                        {
                                            D++;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (drData[0][Day + 9].ToString() == "2")
                                {
                                    D++;
                                }
                            }
                        }
                    }
                    else if (drD[i][6].ToString() == "文字表格")
                    {
                        double Max = Convert.ToDouble(drD[i]["MaxTarget"].ToString());
                        double Min = Convert.ToDouble(drD[i]["MinTarget"].ToString());
                        //等于1代表单行文字表格，等于2代表双行文字表格，
                        if (drD[i][7].ToString() == "1")
                        {
                            //在记录数据的表里查询是否有内容，有内容才继续
                            DataRow[] drData = dt2.Select("ControlOrder=" + drD[i][3].ToString());
                            if (drData.Length > 0)
                            {
                                if (drData[0][Day + 9].ToString() != "")
                                {
                                    if (Min != 0 && Max == 0)
                                    {
                                        //只有最小值时
                                        if (Convert.ToDouble(drData[0][Day + 9].ToString()) < Min)
                                        {
                                            D++;
                                        }
                                    }
                                    else if (Min == 0 && Max != 0)
                                    {
                                        if (Convert.ToDouble(drData[0][Day + 9].ToString()) > Max)
                                        {
                                            D++;
                                        }
                                    }
                                    else if (Min != 0 && Max != 0)
                                    {
                                        if (Convert.ToDouble(drData[0][Day + 9].ToString()) < Min || Convert.ToDouble(drData[0][Day + 9].ToString()) > Max)
                                        {
                                            D++;
                                        }
                                    }
                                }
                            }
                        }
                        //等于1代表1行文字表格，等于2代表2行文字表格，
                        else if (drD[i][7].ToString() == "2")
                        {
                            string ctName = drD[i][4].ToString();
                            DataRow[] isChart = dt1.Select($"ControlName = '{ctName}_Comment'");
                            if (isChart.Length > 0)
                            {
                                //在记录数据的表里查询是否有内容，有内容才继续
                                DataRow[] drData = dt2.Select("ControlOrder=" + drD[i][3].ToString());
                                if (drData.Length == 2)
                                {
                                    if (drData[0][Day + 9].ToString() != "")
                                    {
                                        //双行文字
                                        double Val = Convert.ToDouble(drData[1][Day + 9].ToString()) * 100 / Convert.ToDouble(drData[0][Day + 9].ToString());
                                        if (Min != 0 && Max == 0)
                                        {
                                            //只有最小值时
                                            if (Val < Min)
                                            {
                                                D++;
                                            }
                                        }
                                        else if (Min == 0 && Max != 0)
                                        {
                                            if (Val > Max)
                                            {
                                                D++;
                                            }
                                        }
                                        else if (Min != 0 && Max != 0)
                                        {
                                            if (Val < Min || Val > Max)
                                            {
                                                D++;
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                DataRow[] drData = dt2.Select("ControlOrder=" + drD[i][3].ToString());
                                double actual = Convert.ToDouble(drData[1][Day + 9].ToString());
                                double plan = Convert.ToDouble(drData[0][Day + 9].ToString());

                                if (actual < plan)
                                {
                                    D++;
                                }
                            }

                        }
                    }
                }
            //判断C，有双行文字表格，有单行文字表格，有红绿表格
            for (int i = 0; i < drC.Length; i++)
            {
                if (drC[i][6].ToString() == "红绿表格")
                {
                    DataRow[] drData = dt2.Select("ControlOrder=" + drC[i][3].ToString());
                    if (drData.Length > 0)
                    {
                        if (drData[0][Day + 9].ToString() == "2")
                        {
                            C++;
                        }
                    }
                }
                else if (drC[i][6].ToString() == "文字表格")
                {
                    double Max = Convert.ToDouble(drC[i]["MaxTarget"].ToString());
                    double Min = Convert.ToDouble(drC[i]["MinTarget"].ToString());
                    //等于1代表单行文字表格，等于2代表双行文字表格，
                    if (drC[i][7].ToString() == "1")
                    {
                        //在记录数据的表里查询是否有内容，有内容才继续
                        DataRow[] drData = dt2.Select("ControlOrder=" + drC[i][3].ToString());
                        if (drData.Length > 0)
                        {
                            if (drData[0][Day + 9].ToString() != "")
                            {
                                if (Min != 0 && Max == 0)
                                {
                                    //只有最小值时
                                    if (Convert.ToDouble(drData[0][Day + 9].ToString()) < Min)
                                    {
                                        C++;
                                    }
                                }
                                else if (Min == 0 && Max != 0)
                                {
                                    if (Convert.ToDouble(drData[0][Day + 9].ToString()) > Max)
                                    {
                                        C++;
                                    }
                                }
                                else if (Min != 0 && Max != 0)
                                {
                                    if (Convert.ToDouble(drData[0][Day + 9].ToString()) < Min || Convert.ToDouble(drData[0][Day + 9].ToString()) > Max)
                                    {
                                        C++;
                                    }
                                }
                            }
                        }
                    }
                    //等于1代表1行文字表格，等于2代表2行文字表格，
                    else if (drC[i][7].ToString() == "2")
                    {
                        //在记录数据的表里查询是否有内容，有内容才继续
                        DataRow[] drData = dt2.Select("ControlOrder=" + drC[i][3].ToString());
                        if (drData.Length == 2)
                        {
                            if (drData[0][Day + 9].ToString() != "")
                            {
                                //双行文字
                                //之前是百分数
                                //double Val = Convert.ToDouble(drData[1][Day + 9].ToString()) * 100 / Convert.ToDouble(drData[0][Day + 9].ToString());
                                double Val = Convert.ToDouble(drData[1][Day + 9].ToString()) / Convert.ToDouble(drData[0][Day + 9].ToString());
                                if (Min != 0 && Max == 0)
                                {
                                    //只有最小值时
                                    if (Val < Min)
                                    {
                                        C++;
                                    }
                                }
                                else if (Min == 0 && Max != 0)
                                {
                                    if (Val > Max)
                                    {
                                        C++;
                                    }
                                }
                                else if (Min != 0 && Max != 0)
                                {
                                    if (Val < Min || Val > Max)
                                    {
                                        C++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Console.WriteLine("S:" + S + "\r\nQ:" + Q + "\r\nD:" + D + "\r\nC:" + C);
            string SS;
            string QQ;
            string DD;
            string CC;
            if (S > 0)
            {
                SS = "Red";
            }
            else
            {
                SS = "Green";
            }
            if (Q > 0)
            {
                QQ = "Red";
            }
            else
            {
                QQ = "Green";
            }
            if (D > 0)
            {
                DD = "Red";
            }
            else
            {
                DD = "Green";
            }
            if (C > 0)
            {
                CC = "Red";
            }
            else
            {
                CC = "Green";
            }
            //增加SQDC颜色表数据
            string Condition3 = "FactoryID=" + FactoryID + "and DepartmentID=" + DepartmentID + " and LevelID=" + Level + " and Datetime='" + DateTime.Now.AddDays(-1).Date + "' and Del_tag=0";
            DataTable dt3 = SQL.ReturnDataTable2(SystemInfo.SQL, "Color_of_SQDC", Condition3);
            if (dt3.Rows.Count > 0)
            {
                int ID = Convert.ToInt32(dt3.Rows[0]["ID"].ToString());
                SQL.Update(SystemInfo.SQL, "Color_of_SQDC", $"{"S_Color='" + SS}',{"Q_Color='" + QQ}',{"D_Color='" + DD}',{"C_Color='" + CC}'", "ID=" + ID);
            }
            else
            {
                SQL.Insert(SystemInfo.SQL, "Color_of_SQDC", "FactoryID,DepartmentID,LevelID,Datetime,S_Color,Q_Color,D_Color,C_Color,Del_tag", $"{FactoryID},{DepartmentID},{Level},'{DateTime.Now.AddDays(-1).Date}','{SS}','{QQ}','{DD}','{CC}',{0}");
            }
        }

        public string InsertNewLineEvery20Chars(string input)
        {
            if (string.IsNullOrEmpty(input)) return input;

            StringBuilder sb = new StringBuilder();
            int chunkSize = 20;

            for (int i = 0; i < input.Length; i += chunkSize)
            {
                // 获取从当前位置起最多20个字符的子字符串
                if (i + chunkSize < input.Length)
                    sb.AppendLine(input.Substring(i, chunkSize));
                else
                    sb.Append(input.Substring(i)); // 最后一部分不加换行
            }

            return sb.ToString();
        }

        private void label_MouseHover(object sender, EventArgs e)
        {
            Label label = sender as Label;

            if (label != null)
            {
                // 如果 Label 的内容被截断，则显示 ToolTip
                if (TextRenderer.MeasureText(label.Text, label.Font).Width > label.Width)
                {
                    string res = InsertNewLineEvery20Chars(label.Text);
                    tipLabel.SetToolTip(label, res);
                }
                else
                {
                    tipLabel.SetToolTip(label, string.Empty); // 如果没有截断，则不显示 ToolTip
                }
            }
        }

        private void dtp_Date_ValueChanged(object sender, DateTime value)
        {
            DateTime dt = dtp_Date.Value;
            int year = dt.Year;
            int month = dt.Month;

            ClearPanelExceptButton(panel1);
            Year = year;
            Month = month;
            a = 0;
            LoadFrame();
            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "Departments", $"ID = {DepartmentID}");
            if (dt1.Rows.Count > 0)
            {
                lab_Department.Text = dt1.Rows[0][1].ToString();
            }
            else
            {
                lab_Department.Text = "Unknow";
            }
            LoadTopIssues(DateAdd);
            LoadOpenItem();
        }

        private void ClearPanelExceptButton(Panel panel)
        {
            // 使用倒序遍历控件列表，避免在移除控件时索引错乱
            for (int i = panel.Controls.Count - 1; i >= 0; i--)
            {
                Control control = panel.Controls[i];

                // 如果控件不是 Button 类型，则移除
                if (control.Name == "btn_Back" || control.Name == "lab_Department" || control.Name == "dtp_Date")
                {

                }
                else
                {
                    panel.Controls.Remove(control);
                }
            }
        }

        private void Initial_picBox(Control control)
        {
            if (control is DataGridView)
            {
                if (control is UIDatetimePicker)
                {
                    control.Enabled = true;
                }
                else
                {
                    control.Enabled = false;
                }

            }
            else
            {
                var controlCollection = control.Controls;
                foreach (Control subControl in controlCollection)
                {
                    Initial_picBox(subControl);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Initial_picBox(this);
        }

        private void btn_PdcaMark_Click_1(object sender, EventArgs e)
        {
            methodSwitch.instance.ShowDialog();
        }
    }
}